﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace ResourceBlender_Express {
    /// <summary>
    /// Allows applications to be selected for other purposes.
    /// </summary>
    public partial class WinSelectApplication : Window {
        private bool includeBundles = false;

        /// <summary>
        /// Gets the selected applications.
        /// </summary>
        /// <value>The selected applications.</value>
        public List<RBApplication> SelectedApplications {
            get {
                List<RBApplication> selectedApps = new List<RBApplication>();
                foreach(RBApplication app in lstApplications.SelectedItems) {
                    selectedApps.Add(app);
                }
                return selectedApps;
            }
        }

        /// <summary>
        /// Initializes a new <see cref="WinSelectApplication"/>.
        /// </summary>
        /// <param name="caption">The window caption.</param>
        /// <param name="saveButtonText">The text on the save button.</param>
        /// <param name="includeBundles"><c>true</c> if bundles data should be loaded when loading applications. This required an additional database query.</param>
        public WinSelectApplication(string caption, string saveButtonText, bool includeBundles) {
            InitializeComponent();
            this.Title = caption;
            this.btnSave.Content = saveButtonText;
            this.includeBundles = includeBundles;
        }

        /// <summary>
        /// Returns a dialog result of true and closes the window.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Save(object sender, RoutedEventArgs e) {
            if(lstApplications.SelectedItems.Count == 0) {
                DialogBox.ShowAlert(this, "At least one application must be selected", "Error");
            } else {
                DialogResult = true;
                Close();
            }
        }

        /// <summary>
        /// Closes the window.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CloseWindow(object sender, RoutedEventArgs e) {
            Close();
        }

        /// <summary>
        /// Handles the Loaded event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e) {
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (s, dwe) => {
                Database db = new Database();
                try {
                    dwe.Result = db.LoadApplications(includeBundles);
                } catch(Exception ex) {
                    dwe.Result = "Error connecting to the database: " + ex.Message;
                } finally {
                    if(db != null) {
                        try {
                            db.CloseConnection();
                        } catch { }
                    }
                }
            };
            worker.RunWorkerCompleted += (s, rwe) => {
                SetStatus(null, false);
                if(rwe.Result != null && rwe.Result is string) {
                    DialogBox.Show(this, rwe.Result.ToString(), "Database Error", DialogBox.DialogBoxType.OK, DialogBox.DialogBoxIcon.Error);
                } else {
                    lstApplications.ItemsSource = rwe.Result as List<RBApplication>;
                }
            };
            SetStatus("Loading applications...", true);
            worker.RunWorkerAsync();
        }

        /// <summary>
        /// Sets the text on the status message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="showProgress"><c>true</c> if the progress bar should be shown, otherwise <c>false</c>.</param>
        private void SetStatus(string message, bool showProgress) {
            prgStatus.Visibility = showProgress ? Visibility.Visible : Visibility.Hidden;
            if(message == null) {
                lblStatus.Visibility = Visibility.Hidden;
            } else {
                lblStatus.Visibility = Visibility.Visible;
                lblStatus.Content = message;
            }
        }
    }
}
